/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.errmsg;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.errmsg.ErrMsg;
import com.server.modules.sys.service.errmsg.ReturnMsgService;

/**
 * 错误代码管理Controller
 * @author 王磊
 * @version 2016-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/errmsg/errMsg")
public class ErrMsgController extends BaseController {

	@Autowired
	private ReturnMsgService returnMsgService;

	@ModelAttribute
	public ErrMsg get(@RequestParam(required=false) String id) {
		ErrMsg entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = returnMsgService.get(id);
		}
		if (entity == null){
			entity = new ErrMsg();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:errmsg:errMsg:view")
	@RequestMapping(value = {"list", ""})
	public String list(ErrMsg errMsg, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ErrMsg> page = returnMsgService.findPage(new Page<ErrMsg>(request, response), errMsg);
		model.addAttribute("page", page);
		model.addAttribute("errMsg", errMsg);
		return "modules/sys/errmsg/errMsgList";
	}

	@RequiresPermissions("sys:errmsg:errMsg:view")
	@RequestMapping(value = "form")
	public String form(ErrMsg errMsg, Model model) {
		model.addAttribute("errMsg", errMsg);
		return "modules/sys/errmsg/errMsgForm";
	}

	@RequiresPermissions("sys:errmsg:errMsg:edit")
	@RequestMapping(value = "save")
	public String save(ErrMsg errMsg, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, errMsg)){
			return form(errMsg, model);
		}
		returnMsgService.save(errMsg);
		addMessage(redirectAttributes, "保存错误代码成功");
		return "redirect:"+Global.getAdminPath()+"/sys/errmsg/errMsg/?repage";
	}
	
	@RequiresPermissions("sys:errmsg:errMsg:edit")
	@RequestMapping(value = "delete")
	public String delete(ErrMsg errMsg, RedirectAttributes redirectAttributes) {
		returnMsgService.delete(errMsg);
		addMessage(redirectAttributes, "删除错误代码成功");
		return "redirect:"+Global.getAdminPath()+"/sys/errmsg/errMsg/?repage";
	}

}